package com.healthcloud.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.healthcloud.dao.InstrumentInfoMapper;
import com.healthcloud.dto.res.PatientInstrumentDto;
import com.healthcloud.entity.InstrumentInfoEntity;
import com.healthcloud.service.InstrumentInfoService;
import com.healthcloud.utils.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zj
 * 2017年7月20日 下午4:43:57
 */
@Service
@Transactional
public class InstrumentInfoServiceImpl implements InstrumentInfoService {

    @Autowired
    private InstrumentInfoMapper instrumentInfoMapper;

    @Override
    public void add(InstrumentInfoEntity entity) {
        InstrumentInfoEntity oldInfo = getBySn(entity.getSn());
        if (oldInfo != null) {//当没有设备记录时新增
            return;
        } else {
            instrumentInfoMapper.save(entity);
        }
    }

    @Override
    public List<InstrumentInfoEntity> findByEntityAll(InstrumentInfoEntity entity) {
        return instrumentInfoMapper.findByEntityAll(entity);
    }

    @Override
    public InstrumentInfoEntity getBySn(String sn) {
        return instrumentInfoMapper.getBySn(sn);
    }

    @Override
    public Integer totalByHospitalIds(String ids) {
        return instrumentInfoMapper.totalByHospitalIds(ids);
    }


    /**
     * 获取医院的已租设备
     * @param ids
     * @return
     */
    @Override
    public List<String> listTotalByHospitalIds(String ids) {
        return instrumentInfoMapper.listTotalByHospitalIds(ids);
    }


    @Override
    public Integer useTotalByHospitalIds(String ids, int day) {
        return instrumentInfoMapper.useTotalByHospitalIds(ids, day);
    }

    /**
     * 医院最近使用的设备
     *
     * @param ids
     * @param day
     * @return
     */
    @Override
    public List<String> listUseTotalByHospitalIds(String ids, int day) {
        return instrumentInfoMapper.listUseTotalByHospitalIds(ids, day);
    }

    @Override
    public Integer useTimesByHospitalIds(String ids, int day) {
        return instrumentInfoMapper.useTimesByHospitalIds(ids, day);
    }

    /**
     * 医院最近使用设备
     *
     * @param ids
     * @param day
     * @return
     */
    @Override
    public List<String> listUseTimesByHospitalIds(String ids, int day) {
        return instrumentInfoMapper.listUseTimesByHospitalIds(ids, day);
    }

    @Override
	public List<PatientInstrumentDto> listInfosBySns(List<String> snList) {
    	return instrumentInfoMapper.listInfosBySns(snList);
	}

    @Override
	public List<PatientInstrumentDto> listUnUseInstrument(String hospIds, Integer days) {
        List<String> allSnList = this.listTotalByHospitalIds(hospIds);

        List<String> useSnList = this.listUseTotalByHospitalIds(hospIds, days);
        List<String> unUseSnList = (List<String>) CollUtil.disjunction(allSnList, useSnList);
        List<PatientInstrumentDto> unUseInfoList = this.listInfosBySns(unUseSnList);
        return unUseInfoList;
	}

    @Override
    public List<PatientInstrumentDto> pageForUnUseInstrument(String hospIds, Integer days, PageParam pageParam) {
        List<String> allSnList = this.listTotalByHospitalIds(hospIds);

        List<String> useSnList = this.listUseTotalByHospitalIds(hospIds, days);
        List<String> unUseSnList = (List<String>) CollUtil.disjunction(allSnList, useSnList);
        return instrumentInfoMapper.pageForInfosBySns(unUseSnList, pageParam);
    }

}